package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.dao.Feedback;
import com.kt.uokos.yimo.example.FeedbackExample;
import com.kt.uokos.yimo.mapper.FeedbackMapper;
import com.kt.uokos.yimo.service.FeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Resource
    private FeedbackMapper feedbackMapper;

    @Override
    public List<Feedback> querySelective(Integer id, String userName, Integer pager, Integer limit, String sort, String order) {

        FeedbackExample example = new FeedbackExample();
        FeedbackExample.Criteria criteria = example.createCriteria();

        if (id != null){
            criteria.andIdEqualTo(id);
        }
        if (!StringUtils.isEmpty(userName)){
            criteria.andUsernameLike("%"+userName+"%");
        }

        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
        example.setOrderByClause(sort+" "+order);
        }
        PageHelper.startPage(pager,limit);

        return feedbackMapper.selectByExample(example);
    }

    @Override
    public Integer add(Feedback feedback) {
        feedback.setAddTime(LocalDateTime.now());
        feedback.setUpdateTime(LocalDateTime.now());
        feedback.setDeleted(false);
        return feedbackMapper.insert(feedback);
    }
}
